<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use App\Models\Business;
use Illuminate\Http\Request;

class BusinessController extends CommonController
{

    public function create(Request $request)
    {
        $name = $request['name'];
        $owner = $request['owner'];
        $mobile = $request['mobile'];
        $introduce = $request['introduce'];
        $cover_img = $request['cover_img'];
        $pics = $request['data'];

        $row_info = Business::where('name', $name)->first();
        if ($row_info) {
            return response()->json(['status' => 0, 'msg' => '该商铺已存在！']);
        }

        $data = [
            'name' => $name,
            'owner' => $owner,
            'mobile' => $mobile,
            'introduce' => $introduce,
            'cover_img' => $cover_img,
            'pics' => $pics,
            'status' => 0,
        ];

        $ret = Business::create($data);
        if (!$ret->id) {
            return response()->json(['status' => 0, 'msg' => '发布失败！']);
        }

        return response()->json(['status' => 1, 'msg' => '发布成功！等待审核']);

    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {

        $page = !$request['page'] ? $request['page'] : 1;
        $start = ($page - 1) * self::$totalList;

        $lists = Business::where('status', 1)->orderBy('id', 'desc')->offset($start)->limit(self::$totalList)->get();

        $totalCount = Business::where('status', 1)->count();

        return response()->json(['status' => 1, 'data' => ['list' => $lists, 'totalCount' => $totalCount]]);
    }

    /**
     * Notes:详情
     * User: Administrator
     * Date: 2023/10/9
     * Time: 18:33
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function read(Request $request)
    {
        $id = $request['id'];
        $row_info = Business::where('status', 1)->find($id);
        if (!$row_info) {
            return response()->json(['status' => 0, 'msg' => '参数错误']);
        }

        return response()->json(['status' => 1, 'data' => $row_info]);
    }

}
